package com.qigao.mall.service.impl;

import com.qigao.mall.api.AddrService;
import com.qigao.mall.commons.dto.AddrCreateDto;
import com.qigao.mall.commons.dto.AddrModiDto;
import com.qigao.mall.commons.dto.AddrResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.enums.YesNoEnum;
import com.qigao.mall.jdbc.mapper.TbUserAddrMapper;
import com.qigao.mall.jdbc.model.TbUserAddr;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Copyright (C), 2019-2019, 三人行工作室
 * FileName: AddrServiceImpl
 * Author:   luiz
 * Date:     2019/11/10 16:50
 * Description:收件人地址服务类
 * History:
 * <author>          <time>          <version>          <desc>
 * luiz            2019/11/10 16:50       版本号              描述
 */
@Service
public class AddrServiceImpl implements AddrService {
    @Resource
    private TbUserAddrMapper tbUserAddrMapper;
    @Override
    public Result<List<AddrResultDto>> findAddrList(String userNo) {
        List<TbUserAddr> tbUserAddrs=tbUserAddrMapper.selectByUserNo(userNo);
        if(tbUserAddrs==null || tbUserAddrs.size()==0){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        List<AddrResultDto> addrResultDtos=new ArrayList<>();
        for(TbUserAddr tbUserAddr:tbUserAddrs){
            AddrResultDto addrResultDto=singleCopy(tbUserAddr);
            addrResultDtos.add(addrResultDto);
        }
        return Result.newSuccess(addrResultDtos);
    }

    private AddrResultDto singleCopy(TbUserAddr tbUserAddr) {
        AddrResultDto addrResultDto=new AddrResultDto();
        BeanUtils.copyProperties(tbUserAddr,addrResultDto);
        if(StringUtils.isNotEmpty(tbUserAddr.getProvinceCode())){
            int lastIndex=tbUserAddr.getProvinceCode().lastIndexOf("-");
            addrResultDto.setRecvProvinceCode(tbUserAddr.getProvinceCode());
            if(lastIndex!=-1)
            addrResultDto.setRecvProvinceCode(tbUserAddr.getProvinceCode().substring(0,lastIndex));
        }
        if(StringUtils.isNotEmpty(tbUserAddr.getCityCode())){
            int lastIndex=tbUserAddr.getCityCode().lastIndexOf("-");
            addrResultDto.setRecvCityCode(tbUserAddr.getCityCode());
            if(lastIndex!=-1)
            addrResultDto.setRecvCityCode(tbUserAddr.getCityCode().substring(0,lastIndex));
        }
        if(StringUtils.isNotEmpty(tbUserAddr.getDistrictCode())){
            int lastIndex=tbUserAddr.getDistrictCode().lastIndexOf("-");
            addrResultDto.setRecvDistrictCode(tbUserAddr.getDistrictCode());
            if(lastIndex!=-1)
                addrResultDto.setRecvDistrictCode(tbUserAddr.getDistrictCode().substring(0,lastIndex));
        }
        return addrResultDto;
    }

    @Override
    public void createAddr(AddrCreateDto addrCreateDto) {
        TbUserAddr tbUserAddr=new TbUserAddr();
        BeanUtils.copyProperties(addrCreateDto,tbUserAddr);
        tbUserAddr.setStatus(YesNoEnum.YES.getCode());
        tbUserAddr.setCreateNo(addrCreateDto.getUserNo());
        tbUserAddr.setCreateTime(new Date());
        tbUserAddr.setModiNo(addrCreateDto.getUserNo());
        tbUserAddr.setModiTime(new Date());
       // tbUserAddr.setIfDefault(YesNoEnum.YES.getCode());
        updateOtherDefault();
        tbUserAddrMapper.insertSelective(tbUserAddr);
    }

    private void updateOtherDefault() {
        tbUserAddrMapper.updateOtherDefault();
    }

    @Override
    public void modiAddr(AddrModiDto addrModiDto) {
        TbUserAddr tbUserAddr=new TbUserAddr();
        BeanUtils.copyProperties(addrModiDto,tbUserAddr);
        tbUserAddr.setId(addrModiDto.getId());
        tbUserAddr.setModiNo(addrModiDto.getUserNo());
        tbUserAddr.setModiTime(new Date());
        tbUserAddr.setIfDefault(addrModiDto.getIfDefault());
        if(YesNoEnum.YES.getCode().equals(addrModiDto.getIfDefault())){
            updateOtherDefault();
        }
        tbUserAddrMapper.updateByPrimaryKeySelective(tbUserAddr);
    }

    @Override
    public Result deleteById(String addrId) {
         tbUserAddrMapper.deleteByPrimaryKey(Integer.valueOf(addrId));
        return Result.newSuccess();
    }

    @Override
    public Result<AddrResultDto> findAddrById(String id) {
        TbUserAddr tbUserAddr=tbUserAddrMapper.selectByPrimaryKey(Integer.valueOf(id));
        if(tbUserAddr==null){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        AddrResultDto addrResultDto=  singleCopy(tbUserAddr);
        return Result.newSuccess(addrResultDto);
    }

    @Override
    public Result<AddrResultDto> findDefaultAddr(String userNo) {
        TbUserAddr tbUserAddr=tbUserAddrMapper.selectDefaultAddr(userNo);
        if(tbUserAddr==null){
            tbUserAddr=tbUserAddrMapper.selectTopOne(userNo);
        }
        if(tbUserAddr==null){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        AddrResultDto addrResultDto=  singleCopy(tbUserAddr);
        return Result.newSuccess(addrResultDto);
    }
}
